import store from '@/store'
import axios from 'axios'
import Loker from 'lockr'
import {LOGIN_USER_INFO, LOGIN_AUTH_INFO, X_ACCESS_TOKEN, EXPIRES_IN, REFRESH_TOKEN} from '../config/sysconsts'

export function getCacheLoginUserInfo () {
  return Loker.get(LOGIN_USER_INFO)
}

export function setCacheLoginUserInfo (loginUserInfo) {
  return Loker.set(LOGIN_USER_INFO, loginUserInfo)
}

export function setCacheAuthInfoInfo (authInfo) {
  return Loker.set(LOGIN_AUTH_INFO, authInfo)
}

export function setAccessToken (data) {
  Loker.set(X_ACCESS_TOKEN, data.token)
  Loker.set(EXPIRES_IN, data.expiresIn)
  Loker.set(REFRESH_TOKEN, data.refreshToken)
}

export function getSidebarStatus () {
  Loker.get('sidebarStatus')
}

export function setSidebarStatus (status) {
  Loker.set('sidebarStatus', status)
}

export function clearCacheLogin () {
  Loker.rm(X_ACCESS_TOKEN)
  Loker.rm(EXPIRES_IN)
  Loker.rm(REFRESH_TOKEN)
  Loker.rm(LOGIN_AUTH_INFO)
  return Loker.rm(LOGIN_USER_INFO)
}

export function getLocalStorageCache (key) {
  return Loker.get(key)
}

export function updateAxiosCache () {
  axios.defaults.headers[X_ACCESS_TOKEN] = getLocalStorageCache(X_ACCESS_TOKEN)
  store.dispatch('user/GetUserInfo')
}

